package com.zplus.managers.petservices.services.impls;

import com.zplus.managers.petservices.entities.AdoptRequest;
import com.zplus.managers.petservices.entities.Pet;
import com.zplus.managers.petservices.mappers.AdoptRequestMapper;
import com.zplus.managers.petservices.services.AdoptRequestSerivces;
import com.zplus.managers.petservices.services.PetService;
import com.zplus.managers.petservices.services.RequestState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;

@Service
public class AdoptRequestServiceImpl implements AdoptRequestSerivces
{
    @Autowired
    private AdoptRequestMapper arMapper;

    @Autowired
    private PetService petService;
    @Override
    public boolean newAdoptReqest(AdoptRequest ar)
    {
        // create time stemp
        Date date = new Date(System.currentTimeMillis());
        ar.setRequestTime(date);
        // change pet status to 1
        Pet pet = petService.queryPetById(ar.getPetId());
        if( pet.getState() != 0)
            return false;
        ar.setReqState(1);// 1 waiting for check
        if(!petService.updatePetInfo(pet))
            return false;
        int i = arMapper.insertAdoptRequest(ar);
        return i > 0;
    }

    @Override
    public boolean updateRequestState(AdoptRequest ad, RequestState state)
    {
        int st = 1;
        switch(state)
        {
            case YES:
                st = 2;
                break;
            case NO:
                st = 0;
                break;
            case WAIT:
                st = 1;
                break;
        }
        // update pet status
        Pet pet = petService.queryPetById(ad.getPetId());
        pet.setState(st);
        ad.setReqState(st);
        petService.updatePetInfo(pet);
        // update adopt request info
        int i = arMapper.updateRequest(ad);
        return i > 0;
    }

    @Override
    public boolean deleteRequest(AdoptRequest req)
    {
        int i = arMapper.dropRequest(req);
        return i > 0;
    }

    @Override
    public List<AdoptRequest> getRequsetByDate(String date)
    {
        List<AdoptRequest> adoptRequests = arMapper.queryRequestByDate(date);
        return adoptRequests;
    }

    @Override
    public AdoptRequest getReqeustById(int id)
    {
        return arMapper.queryRequestById(id);
    }

    @Override
    public List<AdoptRequest> getAllRequest(String state)
    {
        List<AdoptRequest> lst = null;
        boolean isOk = false;
        switch(state)
        {
            case "uncheck": // 1
                lst = arMapper.queryALlRequest(1);
                if (!lst.isEmpty())
                    isOk = true;
                break;
            case "pass": // 2
                lst = arMapper.queryALlRequest(2);
                if(!lst.isEmpty())
                    isOk = true;
                break;
            case "reject": // 0
                lst = arMapper.queryALlRequest(0);
                if(!lst.isEmpty())
                    isOk = true;
                break;
            default:
                isOk = false;
                break;
        }
        if(isOk)
            return lst;
        else
            return null;
    }
}
